// Copyright Leonid Shlyapnikov 2010
// Distributed under the GNU Lesser General Public License http://www.gnu.org/licenses/lgpl.html
//    author: Leonid Shlyapnikov, created on: Feb 13, 2010

#ifndef FIXPARSER_CPP_SRC_MAIN_FIXPARSER_UTIL_H_
#define FIXPARSER_CPP_SRC_MAIN_FIXPARSER_UTIL_H_

#include <boost/static_assert.hpp>

#include <cstring>
#include "./MemoryArea.h"
#include "./String.h"

namespace fixparser {


template<size_t N>
size_t static_strlen(const char (&dummy)[N]) {
    BOOST_STATIC_ASSERT(sizeof(dummy) > 0);
    return N - 1;
}


template<size_t N>
char* static_strncpy(char* dest, const char (&src)[N]) {
    return  std::strncpy(dest, src, N);
}


class noninstantiatable {
private:
    noninstantiatable();
    ~noninstantiatable();
};


template<size_t N>
String* newString(MemoryArea* memoryArea, const char (&value)[N]) {
    String* result = memoryArea->create<String>();
    result->assign(value, (N-1));
    return result;
}

template<size_t N>
const String& newStringConst(MemoryArea* memoryArea, const char (&value)[N]) {
    String* result = memoryArea->create<String>();
    result->assign(value, (N-1));
    return *result;
}

inline
unsigned int calculateChecksum(const String& str) {
    unsigned int result = 0;

    const size_t bufferLen = str.length();
    const char* buffer = str.c_str();

    for (size_t indx = 0; indx < bufferLen; indx++) {
        const unsigned char ch = buffer[indx];
        result += ch;
    }

    return result;
}

}

#endif  // FIXPARSER_CPP_SRC_MAIN_FIXPARSER_UTIL_H_
